<template>
    <div id="main">
        <div class="bar">
            <div class="left">
                会员列表
            </div>
            <div class="right">
                <el-button type="success" size="mini"  @click="addMenu"> <i class="el-icon-plus"></i> 添加会员</el-button>
            </div>
        </div>
        <div class="search">
            <el-input placeholder="请输入要搜索的会员名称或手机号码" v-model="username" class="input-with-select">
                
                <el-button slot="append" icon="el-icon-search" @click="searchArt"></el-button>
            </el-input>
        </div>
        <div class="table">
            <el-table
                :data="tableData"
                style="width: 100%;margin-bottom: 20px;"
                row-key="id"
                border
                default-expand-all
                :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                <el-table-column
                prop="userid"
                label="ID"
                align="center"
                sortable
               >
                </el-table-column>
                <el-table-column
                prop="nickname"
                align="center"
                label="会员名称"
                sortable
                >
                </el-table-column>
                <el-table-column
                prop="username"
                align="center"
                label="账户">
                </el-table-column>

                <el-table-column
                prop="name"
                align="center"
                label="班级">
                </el-table-column>
                
 
                <el-table-column
                prop="status"
                align="center"
                label="状态">
                    <template slot-scope="scope">
                        <el-button :type="scope.row.status == 1 ? 'success': 'danger' " size="mini" @click="changeStatus(scope.row.id)">{{ scope.row.status == 1 ? "开启" : "关闭" }}</el-button>
                    </template>
                </el-table-column>

                <el-table-column
                width="300"
                align="center"
                label="操作">
                    <template slot-scope="scope">
                        <el-button type="success" size="mini" @click="showKe(scope.row)">课程</el-button>
                        <el-button type="primary" size="mini" @click="editMenu(scope.row)">编辑</el-button>
                        <el-button type="danger" size="mini" @click="deleteUser(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="page">
            <el-pagination
                background
                layout="prev, pager, next"
                :page-size="pageSize"
                @current-change="pageChange"
                :total="totalNum">
                </el-pagination>
        </div>

        <!-- dialog -->
        <mem-dialog :DialogData="DialogData" :FormData="FormData" @update="getAllMember"></mem-dialog>

    </div>
</template>

<script>
    import MemDialog from "@/components/dialog/MemDialog"

    export default {
        data() {
            return {
                tableData: [],
                DialogData: {
                    title: "",
                    show: false,
                },
                FormData: {},
                username: "",
                pageSize: 10,
                totalNum: 0,
                currentPage: 1,
                
            }
        },
        components: {
            MemDialog,
        },
        computed: {
            token() {
                return localStorage.token; 
            }
        },
        mounted() {
            this.getAllMember()
        },
        methods: {
            // 获取所有会员
            async getAllMember( page ) {
                let pagema = page ? page : 1;
                let res = await this.$axios.post("/admin/user/member", { token: this.token, page: pagema })
                 console.log(res);
                if(res.data.code == 200) {
                    this.tableData = res.data.data
                    this.totalNum = this.tableData[0].count
                }
                
            },
            // 编辑菜单
            editMenu( user ) {
                this.DialogData = {
                    show: true,
                    title: "编辑菜单",
                    add: false,
                    edit: true
                }
                this.FormData = user
            },
            // 添加菜单
            addMenu() {
                this.DialogData = {
                    show: true,
                    title: "添加菜单",
                    add: true,
                    edit: false
                }
                this.FormData = {}
            },
            // 菜单状态改变
            async changeStatus( id ) {
                let res = await this.$axios.post("", {token: this.token, id })
                // console.log(res);
                if(res.data.code == 204) {
                    this.$message.success("更新成功")
                    this.getAllMenu()
                } else {
                    this.$message.success("更新失败")
                    this.getAllMenu()
                }
                
            },
            // 删除操作
            async deleteUser( user ) {
                // console.log(user);
                let res = await this.$axios.post("/admin/user/memberremove", { token: this.token, userid: user.userid })
                console.log(res);
                let code = res.data.code
                if (code == 202) {
                    this.$message.success("操作成功")
                    this.getAllMember()
                } else {
                    this.$message.error("操作失败")
                    this.getAllMember()
                }
                
            },
            // 查看课程
            showKe( user ) {
                this.$router.push(`/userkelist/${user.userid}`)
            },
            // 搜索会员
            async searchArt() {
                if (this.username) {
                    this.tableData = []
                    let res = await this.$axios.post("/admin/user/membersou", { username: this.username, token: this.token })
                    // console.log(res);
                    let code = res.data.code
                    if (code == 200) {
                        this.tableData = res.data.data
                    } else {
                        this.$message.error("未搜索到相关会员")
                        this.getAllMember()
                    }
                } else {
                    this.$message.error("请输入搜索内容")
                    return false
                }
            },
            // 页码变化
            pageChange( e ) {
                this.getAllMember(e)
            }
        },
    }
</script>

<style lang="scss" scoped>
 #main {
     width: 100%;
     box-sizing: border-box;
     .bar {
         width: 100%;
         font-weight: bold;
         display: flex;
         justify-content: space-between;
         height: 40px;
         line-height: 40px;
         border-bottom: 1px solid #aaa;
     }
     .search {
         padding: 5px 0;
     }
     .page {
         width: 100%;
         text-align: right;
     }
     .table {
         margin-top: 20px;
     }
     .pic {
         width: 40px;
         height: 40px;
         display: block;
     }
 }
</style>